﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using Sdepci.InfoCode.BLL.Authority;
using Sdepci.InfoCode.BLL.CodeDesign;

namespace Sdepci.InfoCode.Web.Interface
{
    public partial class CreateInfoCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindData();
            }
        }

        private void BindData()
        {
            bool validate = false;

            if (this.Request.QueryString["RuleGuid"] != null && this.Request.QueryString["RuleGuid"].Trim().Length > 0)
            {
                // 验证RuleGuid是否合法
                if (new TypeRuleManager().Exist(this.Request.QueryString["RuleGuid"].Trim()))
                {
                    TypeRule rule = new TypeRule(this.Request.QueryString["RuleGuid"].Trim());
                    this.lblTitle.Text = rule.RuleName;
                    this.lblRuleDemo.Text = rule.RuleDemo;
                    this.RuleGuid.Value = rule.RuleGuid;
                    this.RuleModule.Value = rule.RuleModule;

                    validate = true;
                }
            }

            if (!validate)
            {
                this.Response.Clear();
                this.Response.Write("<script language=\"javascript\">alert(\"参数错误！请与系统管理员联系。\");self.close();</script>");
                this.Response.End();
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            this.Response.Clear();
            this.Response.Write(this.Create());
            this.Response.End();
        }

        private string Create()
        {
            string infoCode, infoDesc, serialReference, serialNumber;

            // 判断值的有效性
            if (!"".Equals(this.InfoCode.Value))
            {
                infoCode = this.InfoCode.Value;
                infoDesc = this.InfoDesc.Text;
            }
            else
            {
                return "<script language=\"javascript\">alert(\"系统错误：编码为空，未能获取！请与系统管理员联系。\");self.close();</script>";
            }

            if (!"".Equals(this.SerialReference.Value))
            {
                serialReference = this.SerialReference.Value;
            }
            else
            {
                return "<script language=\"javascript\">alert(\"系统错误：流水号参考码为空，未能获取！请与系统管理员联系。\");self.close();</script>";
            }

            if (!"".Equals(this.SerialNumber.Value))
            {
                serialNumber = this.SerialNumber.Value;
            }
            else
            {
                return "<script language=\"javascript\">alert(\"系统错误：当前流水号为空，未能获取！请与系统管理员联系。\");self.close();</script>";
            }

            this.Session.Add("InfoCode", infoCode);
            this.Session.Add("InfoDesc", infoDesc);
            this.Session.Add("SerialReference", serialReference);
            this.Session.Add("SerialNumber", serialNumber);

            return "<script language=\"javascript\">self.close();</script>";
        }
    }
}
